package com.zkthink.ceres.store.service.impl;

import static com.zkthink.ceres.common.constant.CacheKey.buildStorePointKey;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zkthink.base.request.PageParams;
import com.zkthink.base.service.SuperServiceImpl;
import com.zkthink.cache.repository.RedisRepositoryImpl;
import com.zkthink.ceres.common.enums.StoreDailyStatisticEnum;
import com.zkthink.ceres.store.dao.StoreDailyStatisticMapper;
import com.zkthink.ceres.store.entity.Store;
import com.zkthink.ceres.store.entity.StoreDailyStatistic;
import com.zkthink.ceres.store.service.StoreDailyStatisticService;
import com.zkthink.ceres.store.service.StoreService;
import com.zkthink.context.BaseContextHandler;
import com.zkthink.database.mybatis.conditions.Wraps;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * 业务实现类 店铺每日统计表
 * </p>
 *
 * @author kellen
 * @date 2020-05-31
 */
@Slf4j
@Service
@AllArgsConstructor
public class StoreDailyStatisticServiceImpl extends
    SuperServiceImpl<StoreDailyStatisticMapper, StoreDailyStatistic> implements
    StoreDailyStatisticService {

  private StoreService storeService;

  private RedisRepositoryImpl redisRepository;

  @Override
  public List<StoreDailyStatistic> countStoreDailyData(Map<String, Object> param) {
    return this.getBaseMapper().countStoreDailyData(param);
  }

  @Override
  public void dailyStatistic() {
    //查询所有的店铺信息
    LocalDate trxDate = LocalDate.now();
    PageParams pageParams = new PageParams();
    pageParams.setSize(1000);
    IPage<Store> storeIPage = storeService.findStoreListForPlatform(pageParams);
    List<Store> storeList = storeIPage.getRecords();
    if (!CollectionUtils.isEmpty(storeList)) {
      storeList.forEach(store -> {
        String tenantCode = storeService.findTenantCodeByStoreId(store.getId());
        BaseContextHandler.setTenant(tenantCode);
        Integer storeVisitCount = redisRepository
            .get(buildStorePointKey(StoreDailyStatisticEnum.PV, store.getId()));
        Integer storeAddCartCount = redisRepository
            .get(buildStorePointKey(StoreDailyStatisticEnum.ADD_CART, store.getId()));
        Integer storeSettleCount = redisRepository
            .get(buildStorePointKey(StoreDailyStatisticEnum.SETTLE, store.getId()));
        Integer storePayCount = redisRepository
            .get(buildStorePointKey(StoreDailyStatisticEnum.PAY, store.getId()));
        Integer storePaySuccessCount = redisRepository
            .get(buildStorePointKey(StoreDailyStatisticEnum.PAY_SUCCESS, store.getId()));
        //查询当天是否有值
        StoreDailyStatistic storeDailyStatistic = this.getOne(Wraps.<StoreDailyStatistic>lbQ()
            .eq(StoreDailyStatistic::getTrxDate, trxDate)
            .eq(StoreDailyStatistic::getStoreId, store.getId())
        );
        if (storeDailyStatistic != null) {
          storeDailyStatistic.setAddCartCount(storeAddCartCount).setPayCount(storePayCount)
              .setPv(storeVisitCount)
              .setSettleCount(storeSettleCount).setPaySuccessCount(storePaySuccessCount);
          this.updateById(storeDailyStatistic);
        } else {
          storeDailyStatistic = StoreDailyStatistic.builder().storeId(store.getId())
              .trxDate(trxDate)
              .addCartCount(storeAddCartCount).payCount(storePayCount)
              .paySuccessCount(storePaySuccessCount).pv(storeVisitCount)
              .settleCount(storeSettleCount).paySuccessCount(storePaySuccessCount).build();
          this.save(storeDailyStatistic);
        }
      });
    }
  }
}
